/*
 * Written by Dustin Striplin, & Phat Le
 */

package TableModels;

import java.util.ArrayList;

/*
 * This class is used by the table. This class defines the format of the table.
 */

import Model.Constants;
import Model.Manuscript;
import Model.Review;

public class TemplateTableModel extends MasterTable{
	
	private static final long serialVersionUID = 1L;
	private int[] editableCols = {7};	//used to determine which columns are editable, in this case  Approval
	private String[] columnNames = {"Title", "Author", "Sub PC", "Reviewers", "Paper Link", "Review Links", "Recommendation", "Approval"};
	private ArrayList<ArrayList<Object>> data;  //2D ArrayList containing the individual cells in the table
	private ArrayList<Manuscript> manuscripts;		//saved to store data that is hidden from the table.
	
	
	
	/**
	 * Creates a table model
	 * @author Dustin Striplin
	 * @param the_data the manuscripts to be stored
	 */
	public TemplateTableModel(final ArrayList<Manuscript> the_data) {
		manuscripts = the_data;
		data = new ArrayList<ArrayList<Object>>();
		storeData(the_data);
	}
	
	/**
	 * @return the column name
	 */
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    /**
     * returns a link to the manuscript.
     * @param row the desired row.
     * @return the link
     */
    public String getManuscriptLink(int row) {
    	return manuscripts.get(row).getLink();
    }
    
    /**
     * Returns an ArrayList with the reviews
     */
    public ArrayList<Review> getReviews(int row) {
    	return manuscripts.get(row).getReviews();
    }
    
	/**
	 * Returns the number of columns
	 */
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}
	
	/**
	 * 	Returns the most specific superclass for all the cell values in the column.
	 */
	public Class<?> 	getColumnClass(int col) {
		return getValueAt(0, col).getClass();
	}

	
	/**
	 * Returns the number of rows.
	 */
	@Override
	public int getRowCount() {
		return data.size();
	}
	
	
	@Override
	public Object getValueAt(int row, int column) {
		return data.get(row).get(column);
	}
	
	
	@Override
	public void setValueAt(Object aValue, int row, int col) {
		data.get(row).set(col, aValue);
		fireTableCellUpdated(row, col);
	}
	
	/**
	 * @author Dustin Striplin
	 * Returns true if a column is editable, false otherwise.
	 */
	@Override
	public boolean isCellEditable(int row, int col) {
		//checks the editableCols array for columns that are editable
		for(int i: editableCols) {
			if(i == col)
				return true;
		}
		return false;
	}
	
	/**
	 * Stores all of the data a program chair would need, I chose program chair 
	 * because it is the most complicated.
	 * @author Phat Le
	 * @author Dustin Striplin
	 * @param data the manuscripts to be stored
	 */
	private void storeData(final ArrayList<Manuscript> the_data) {
		int i = 0;
		for(Manuscript m: the_data) {  //Represents a row
			data.add(new ArrayList<Object>());	//create a new ArrayList that represents a single paper
			data.get(i).add(m.getTitle());		//All of the stuff in the for loop represents
			data.get(i).add(m.getAuthor());		//a column in the row.
			data.get(i).add(m.getSubpc());
			String reviewers = "-";
			String reviewLinks = "";
			for(int j = 0; j < m.getReviews().size(); j++) {
				if(j != (m.getReviews().size() - 1)) {
					reviewers +=  m.getReviews().get(j).getReviewer() + ", ";
					reviewLinks += m.getReviews().get(j).getReviewLink() + "\n";
				} else {
					reviewers +=  m.getReviews().get(j).getReviewer();
					reviewLinks += m.getReviews().get(j).getReviewLink();
				}
				
			}
			data.get(i).add(reviewers);
			data.get(i).add(m.getLink());	
			data.get(i).add(reviewLinks);
			//CONVERT TO STRING
			data.get(i).add(Constants.convertItoConst(m.getRecommendation()));
			data.get(i).add(Constants.convertItoConst(m.getApproval()));
			i++;
		}
	}												//TODO, I want the reviewers name to be a link to the reviews.
}													//TODO, I want the Title to be a link to the paper.